<template>
  <!-- 我的收货地址页面 -->
  <view class="main">
    <!-- 无地址 -->
    <view class="wu_Huo" v-if="listData.length < 1">
      <image src="../../imges/zwdz.png" mode="aspectFill"></image>
      <text>暂无收货地址</text>
    </view>
    <view
      class="adress"
      v-for="(item, index) in listData"
      :key="index"
      @tap="goEdit(item)"
    >
      <view class="left" @tap="select(item)">
        <view class="xinxi">
          <text class="name">{{ item.receiveName }}</text>
          <text class="phone">{{ item.receivePhone }}</text>
          <view class="btn" v-if="item.isDefault == 1">默认</view>
        </view>
        <view class="dizhi">
          {{ item.provinceCity + " " + item.fullAddress }}
        </view>
      </view>
      <view class="right" @tap.stop="bianji(item)"> 编辑 </view>
    </view>
    <view class="footer">
      <navigator
        url="/pages/webGoods/addAddress?add=1"
        hover-class="none"
        class="add"
      >
        添加地址
      </navigator>
    </view>
  </view>
</template>

<script>
import request from "../../utils/request.js";
export default {
  data() {
    return {
      listData: [],
      type: "",
    };
  },
  onShow() {
    this.addressList();
  },
  onLoad(option) {
    if (option.type) {
      this.type = option.type;
    }
  },
  methods: {
    addressList() {
      let token = uni.getStorageSync("token");
      const that = this;
      if (!token) {
        uni.showToast({
          title: "请授权登录再操作！",
          duration: 2000,
          icon: "none",
        });
        setTimeout(() => {
          uni.navigateTo({
            url: "/pages/index/index",
          });
        }, 2000);
      } else {
        request({
          url: "app/goods/orderAddressList",
          method: "POST",
          header: {
            "content-type": "application/json;charset=UTF-8",
            "api-client": "72e0fee8172411ea995200163e05ff66",
            "api-version": "1.3.3",
            "Source-Type": 7,
            token: token,
          },
          data: {},
        }).then((res) => {
          if (res.data.code == 200) {
            that.listData = res.data.data;
          } else if (res.data.code == 401) {
            uni.showToast({
              title: "登录失效，请重新授权登录！",
              duration: 2000,
              icon: "none",
            });
            setTimeout(() => {
              uni.navigateTo({
                url: "/pages/index/index",
              });
            }, 2000);
          } else {
            uni.showToast({
              title: res.data.msg,
              duration: 2000,
              icon: "none",
            });
          }
        });
      }
    },
    goEdit(item) {
      if (this.type != 1) {
        uni.navigateTo({
          url:
            "/pages/webGoods/addAddress?id=" +
            item.id +
            "&name=" +
            item.receiveName +
            "&phone=" +
            item.receivePhone +
            "&postCode=" +
            item.postcode +
            "&city=" +
            item.provinceCity +
            "&detailedAddress=" +
            item.fullAddress +
            "&provinceCityCode=" +
            item.provinceCityCode +
            "&isReward=" +
            item.isDefault,
        });
      }
    },
    bianji(item) {
      // if(this.type == 1){
      uni.navigateTo({
        url:
          "/pages/webGoods/addAddress?id=" +
          item.id +
          "&name=" +
          item.receiveName +
          "&phone=" +
          item.receivePhone +
          "&postCode=" +
          item.postcode +
          "&city=" +
          item.provinceCity +
          "&detailedAddress=" +
          item.fullAddress +
          "&provinceCityCode=" +
          item.provinceCityCode +
          "&isReward=" +
          item.isDefault,
      });
      // }
    },
    select(data) {
      if (this.type == 1) {
        uni.setStorage({
          key: "address",
          data: data,
          success: function () {},
        });
        uni.navigateBack({
          delta: 1,
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  padding: 0 32rpx;
  box-sizing: border-box;
}
.wu_Huo {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 300rpx;

  image {
    width: 365rpx;
    height: 260rpx;
  }

  text {
    font-size: 28rpx;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(136, 136, 136, 1);
    line-height: 37px;
    margin-top: 20rpx;
  }
}
.adress {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eeeeee;
  .left {
    padding-right: 20rpx;
    .xinxi {
      display: flex;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      .name {
        font-size: 30rpx;
        color: #333333;
      }
      .phone {
        font-size: 28rpx;
        color: #888888;
        margin: 0 25rpx 0 21rpx;
      }
      .btn {
        width: 72rpx;
        height: 37rpx;
        font-size: 26rpx;
        text-align: center;
        line-height: 37rpx;
        background-color: #ffe9e9;
        color: #ff2e2e;
        border-radius: 4rpx;
      }
    }
    .dizhi {
      font-size: 28rpx;
    }
  }
  .right {
    width: 90rpx;
    height: 56rpx;
    padding-left: 24rpx;
    border-left: 1rpx solid #cccccc;
    line-height: 56rpx;
    font-size: 28rpx;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #888888;
  }
}
.footer {
  width: 100%;
  height: 100rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  bottom: 10rpx;
  left: 0;
  background-color: #ffffff;
  .add {
    width: 686rpx;
    height: 74rpx;
    background: #2168eb;
    border-radius: 46rpx;
    color: #ffffff;
    text-align: center;
    line-height: 74rpx;
    font-size: 26rpx;
  }
}
</style>
